package com.aaa.json.dao;


import com.aaa.json.dao.BaseDao;
import com.aaa.json.entity.Emp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: javaweb-20230304
 * @description:
 * @author: 诸葛龙辰
 * @create: 2023-03-04 14:22
 * @version:
 **/
public class EmpDaoImpl implements EmpDao {
    @Override
    public List<Map> queryByPage(int start, int pageSize, Emp emp) {
        //定义查询、、
        String sql =" select emp_id, emp_name,phone_num,dept_no ,province from tb_emp where 1=1  ";
        //定义list动态查询
        List<String> paramList = new ArrayList();
        if (emp !=null){
            if (emp.getEmpName() !=null &&!"".equals(emp.getEmpName())){
                sql +=" and emp_name like ?";
                paramList.add("%" +emp.getEmpName()+"%");
            }
            if (emp.getProvince()!=null &&!"".equals(emp.getProvince())){
                sql+=" and province like ?";
                paramList.add("%"+emp.getProvince()+"%");
            }
        }
        sql += "limit "+ start+","+pageSize;
        System.out.println(sql+"dsd_____----___----");
        return BaseDao.executQuery(sql,paramList.toArray());
    }

    @Override
    public int queryTotal(Emp emp) {
        String sql = "select count(*)  total from tb_emp where 1=1";
        List<String> paramList = new ArrayList();
        if (emp !=null){
            if (emp.getEmpName() !=null &&!"".equals(emp.getEmpName())){
                sql +=" and emp_name like ?";
                paramList.add("%" +emp.getEmpName()+"%");
            }
            if (emp.getProvince()!=null &&!"".equals(emp.getProvince())){
                sql+=" and province like ?";
                paramList.add("%"+emp.getProvince()+"%");
            }
        }
        System.out.println(sql+"dsd_____----___----");
        //查询
        List<Map> mapList = BaseDao.executQuery(sql,paramList.toArray());
        if (mapList!=null&&mapList.size()>0){
         return Integer.valueOf(mapList.get(0).get("total").toString());
        }

        return 0;
    }

    @Override
    public List<Map> queryAll() {
        return BaseDao.executQuery(
   " select emp_id,emp_name,sex,phone_num,province,hire_date,dept_no,salary,password  from tb_emp");
    }

    @Override
    public List<Map> queryById(int empId) {
        return BaseDao.executQuery(
    " select emp_id,emp_name,sex,phone_num,province,hire_date,dept_no,salary,password  from tb_emp " +
            "where emp_id=?",empId);
    }


    @Override
    public int add(Emp emp) {
        return  BaseDao.executeUpdate("insert into tb_emp(emp_name, sex, phone_num, province,hire_date, dept_no, salary, password) " +
                " values(?,?,?,?,?,?,?,?)",new Object[]{emp.getEmpName(),emp.getSex(),emp.getPhoneNum(),emp.getProvince()
                ,emp.getHireDate(),emp.getDeptNo(),emp.getSalary(),emp.getPassword()});
    }

    @Override
    public int update(Emp emp) {
        return BaseDao.executeUpdate("update tb_emp set emp_name=?, sex=?, phone_num=?, province=?,hire_date=?, dept_no=?, salary=?, password=? where emp_id=?",
                new Object[]{emp.getEmpName(),emp.getSex(),emp.getPhoneNum(),emp.getProvince()
                        ,emp.getHireDate(),emp.getDeptNo(),emp.getSalary(),emp.getPassword(),emp.getEmpId()});
    }

    @Override
    public int deleteById(int empId) {
        return BaseDao.executeUpdate("delete from tb_emp where emp_id=?",new Object[]{empId});
    }

    /**
     * 账号与密码
     */
    @Override
    public List<Map> queryByParam(Emp emp) {
        return BaseDao.executQuery("select emp_id, emp_name, sex, phone_num, province, " +
                        "hire_date, dept_no, salary, password,user from tb_emp where user=? and password=?",
                new Object[]{emp.getUser(),emp.getPassword()});
    }

}
